import BaseView from "../../../Script/Base/BaseView";
import { EBundleType, EResType, EViewType } from "../../../Script/Common/Enum";
import EventID from "../../../Script/Common/EventID";
import { IMessage, ITalkData, IViewData } from "../../../Script/Interface/IMessage";
import { SceneGame } from "../../../Script/SceneGame";
import AppService from "../../../Script/Service/AppService";
import GameUtil from "../../../Script/Util/GameUtil";
import Message from "../../Common/Message";
import { TalkModule } from "../../Common/TalkModule";
import TaskModule from "../../Common/TaskModule";
import StorageUtil from "../../Util/StorageUtil";
import TownUI from "./TownUI";

const { ccclass, property } = cc._decorator;

const VIEW_DATA: IViewData = {
    name: 'PactUI',
    type: EViewType.Panel,
    bundle: EBundleType.Game,
    zIndex: 100,
};

@ccclass
export default class PactUI extends BaseView implements IMessage {

    @property({ type: cc.Label, tooltip: "甲方姓名" })
    private labAName: cc.Label = null;
    @property({ type: cc.Label, tooltip: "乙方姓名" })
    private labBName: cc.Label = null;
    @property({ type: cc.Label, tooltip: "余额" })
    private labMoney: cc.Label = null;
    @property({ type: cc.Label, tooltip: "日期" })
    private labDate: cc.Label = null;
    @property({ type: cc.Node, tooltip: "盖章" })
    private zhang: cc.Node = null;

    private money: number = 0;

    private talkData: ITalkData = null;

    recvMsg(cmd: number, data: any) {
        switch (cmd) {
            case EventID.TEST_CMD:
                {

                }
                break;
        }
    }

    protected onLoad() {
        super.onLoad();
        this.addMessage(EventID.TEST_CMD, this);

        this.talkData = StorageUtil.instance.getTalkData();

        this.initView();
    }

    private initView() {
        console.log(this.prepareData);
        if (!this.prepareData) {
            console.log("PactUI.initView()===>签约数据不存在~");
            return;
        }

        this.zhang.active = false;
        this.money = this.prepareData.money;
        this.labAName.string = `${this.prepareData.a}`;
        this.labBName.string = `${this.prepareData.b}`;
        this.labMoney.string = `${GameUtil.changeUnit(this.money, 0)}`;
        this.labDate.string = `${new Date().getFullYear()}年`;
    }

    private zhangAnim() {
        this.zhang.active = true;
        this.zhang.stopAllActions();
        cc.tween(this.zhang).sequence(
            cc.scaleTo(0, 10),
            cc.scaleTo(0.15, 1),
            cc.callFunc(() => {
                this.talkData.id = 11201;
                TalkModule.updateCheck();
                SceneGame.instance.showToast(cc.v2(0, 0), "恭喜您已签约成功~");
            }),
        ).start();
    }

    private onBtnsClicked(event: cc.Event.EventTouch, eventData: string) {
        const name = event.target.name;
        switch (name) {
            case 'btnClose':
                {
                    if (TownUI.instance && TownUI.instance.node.active) {
                        TownUI.instance.check();
                    }
                    SceneGame.instance.removePopView(this.node);
                }
                break;
            case 'btnOk':
                {
                    if (this.zhang.active) {
                        SceneGame.instance.showToast(cc.v2(0, 0), "您已签约成功~");
                        return;
                    }

                    const gameData = StorageUtil.instance.getGameData();
                    if (gameData.money >= this.money) {
                        // 盖章
                        this.zhangAnim();
                        // 任务数据
                        TaskModule.instance.saveData();
                        GameUtil.instance.changeResValue(EResType.Money, -this.money);
                        Message.instance.send(EventID.REFRESH_TOP_BAR, { type: EResType.Money, value: -this.money });
                    } else {
                        SceneGame.instance.showToast(cc.v2(0, 0), "资金不足~");
                    }
                }
                break;
        }
    }
}
AppService.instance.registViewData(VIEW_DATA);